#include "data_memory.h"

std::unordered_map<std::string, int> DataMemory::data;
std::unordered_map<std::string, int> DataMemory::prev_data;

void DataMemory::set_val(const std::string &var_name, int val)
{
    data[var_name] = val;
}

int DataMemory::get_val_auto(const std::string &var_name)
{
    if (var_name.substr(0, PREV_PREFIX_LEN) == PREV_PREFIX)
        return get_prev_val(var_name.substr(PREV_PREFIX_LEN));
    return get_val(var_name);
}

int DataMemory::get_val(const std::string &var_name)
{
    auto it = data.find(var_name);
    if (it == data.end())
    {
        std::cout << "Error: variable " << var_name << " not found (in data)" << std::endl;
        exit(1);
    }
    return it->second;
}

int DataMemory::get_prev_val(const std::string &var_name)
{
    auto it = prev_data.find(var_name);
    if (it == prev_data.end())
    {
        std::cout << "Error: variable " << var_name << " not found (in prev_data)" << std::endl;
        exit(1);
    }
    return it->second;
}

void DataMemory::save_snapshot()
{
    swap(data, prev_data);
    data.clear();
}

void DataMemory::clear()
{
    data.clear();
    prev_data.clear();
}
